#!/bin/bash
#grab this sites variables/site number
while read line; do
eval $line
done < Config/config

input_file="${1}"
output_file="${2}"
year=$3
if [ $# -ne "3" ]; then
{
	echo "Usage: ${0} \"input file\" \"output file\" [YYYY]"
	exit 1
}
fi

if  [ ! -f "${1}" ]; then
{
	echo "You need to pass an input file."
	echo "The file: \"$1\" dosen't exist?"
	exit 1
}
fi
if  [ -f "${2}" ]; then
{
	echo "This output file already exists."
	echo "Want me to write over it?"
	read wo
	if [[ $wo == [Yy]* ]] ; then
	{
		echo "Running"
		rm -f ${output_file}
	}
	else
	{
		exit 1
		
	}
	fi 

}
fi
#make sure we recognize the format of this file
number_columns=`awk '{print NF}' "${input_file}" | sort -nu | tail -n 1`
if [ $number_columns -ne 7 ]; then
	{
		echo "Sorry...I don't recognize this as API output!"
		echo "Did you remember to trim off the begining of the caputure file so that"
		echo "only the data remained."
		exit 1;
	}
fi
#THIS FUNCTION TAKES THE API JULIAN DATE FORMAT AND RETURNS THE DMS FORMAT TIMESTAMP
jul(){
	translated_date=`date -d "1/1/${4} + $1 days + $2 hours + $3 minutes" '+%m/%d/%Y %H:%M:00'`
	echo "${translated_date}"
}
#PROCESS MINUTE FILE USING 3 VARIABLES
#FIRST: INPUT FILE
#SECOND: OUTPUT FILE
#THIRD: SAROAD
#FORTH: YEAR
run_min(){
	cat "${1}" | while read a b c d e f g
	do
			days=`echo $b | sed 's/:/\ /g' | awk '{ print $1-1 }'`
			#unlike with minute data, need to correct 
			#by subtracting one hour.
			hours=`echo $b | sed 's/:/\ /g' | awk '{ print $2 }'`
			mins=`echo $b | sed 's/:/\ /g' | awk '{ print $3 }'`
			value=`echo $f | sed 's/CONC1=//g'`
			echo "${3}|O3|"`jul $days $hours $mins ${4}`"|0|${value}|" >> ${2}
	done
	}

determine_hourly(){
			clear
			echo "Is this a minute file or hour? (M or H)"
			read type
			
			if [[ $type == [Mm]* ]] ; then
				{
				echo "run as minute"
			run_min ${1} ${2} ${3} ${4}
				}
			else
				{
				echo "run as hourly"
			run_hourly ${1} ${2} ${3} ${4}
	
				}
			fi 

	}
run_hourly(){	
			echo "I think your API400 IDAS is set to record houly data still."
			echo "I need to compensate for this by adding fake minutes equivalent"
			echo " to each hours value. Is that ok?"
			read cnvrthr
				if [[ $cnvrthr == [Yy]* ]] ; then
					{
						echo "Running"
						cat "${1}" | grep -v XXXX | while read a b c d e f g
						do
							#In ordr to get the correct day, we need to subtract
							#an additional day from each
							days=`echo $b | sed 's/:/\ /g' | awk '{ print $1-1 }'`
							#unlike with minute data, need to correct 
							#by subtracting one hour.
							hours=`echo $b | sed 's/:/\ /g' | awk '{ print $2-1 }'`
							mins=`echo $b | sed 's/:/\ /g' | awk '{ print $3 }'`
							value=`echo $f | sed 's/CONC1=//g'`
							echo "${3}|O3|"`jul $days $hours $mins $4`"|0|${value}|" >> ${2}
							for i in `seq -w 01 59`; 
								do
									echo "${3}|O3|"`jul $days $hours $i $year`"|0|${value}|" >> ${2}
								done
						done
						exit 0
					}
				fi
		}

determine_hourly ${input_file} ${output_file} ${saroad} ${year}
exit

#	{
#		echo "run as hourly"
#		run_hourly ${input_file} ${output_file} ${saroad} ${year}
#	}
#	else
#	{
#		echo "run as minutes"
#		run_min ${input_file} ${output_file} ${saroad} ${year}
#	}
#fi
